﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
using static GlobalMembersGame_control;

public class LCUI : MyGraph
{
    public static Dictionary<LCUI_Widget_, object> KeyValuePairs = new Dictionary<LCUI_Widget_, object>();
    public static float Timer;
    public static LCUI_Event Up;
    public static LCUI_Event Down;
    public static LCUI_Event Left;
    public static LCUI_Event Right;
    public static LCUI_Event A;
    public static LCUI_Event B;
    public static LCUI_Event Jump;

    public static float KeyCodeTime;
    
    // Start is called before the first frame update
    void Start()
    {
        Up = new LCUI_Event(GlobalMembersGame_config.game_config.control_key.up);
        Down = new LCUI_Event(GlobalMembersGame_config.game_config.control_key.down);

        Left = new LCUI_Event(GlobalMembersGame_config.game_config.control_key.left);

        Right = new LCUI_Event(GlobalMembersGame_config.game_config.control_key.right);

        A = new LCUI_Event(GlobalMembersGame_config.game_config.control_key.a_attack);

        B = new LCUI_Event(GlobalMembersGame_config.game_config.control_key.b_attack);
        Jump = new LCUI_Event(GlobalMembersGame_config.game_config.control_key.jump);

    }

    // Update is called once per frame
    void Update()
    {
        
       
    }

    public static bool GetAKey()
    {
        return Input.GetKeyDown((KeyCode)GlobalMembersGame_config.game_config.control_key.a_attack);
    }

    public static bool GetBKey()
    {
        return Input.GetKeyDown((KeyCode)GlobalMembersGame_config.game_config.control_key.b_attack);
    }


    public static void LCUISleeper_Create(LCUI_Sleeper wait_time)
    {

    }

    public static int LCUISleeper_StartSleep(LCUI_Sleeper wait_pause, int n_ms)
    {
        return 1;
    }

    public static void LCUIThread_Exit(object obj)
    {

    }

    public static int LCUI_GetTickCount()
    {
        return Environment .TickCount;
    }

    public static int LCUI_GetTicks(int time)
    {
        return Environment.TickCount-time;
    }

    public static  Dictionary<int,funcDelegate> DelayAction = new Dictionary<int, funcDelegate>();

    public void Action(int id)
    {
        DelayAction[id](null);
    }

    public static void LCUITimer_Free(int timeId)
    {
        Timer = 0;
        CandyUtils.DesDelayAction(timeId);
    }

    public static void LCUITimer_Pause(int time)
    {

    }

    public static void LCUITimer_Continue(int time)
    {

    }

    public static int LCUITimer_Set(int n_ms, GlobalMembersGame_control. funcDelegate func, GamePlayer_ player, LCUI_BOOL fALSE)
    {
        return CandyUtils.DelayAction(
            () => {
                func(player);
            }, n_ms/1000f);
        
        //return 1;
    }

    public static int LCUITimer_Set(int n_ms,Action <IntPtr> func, GamePlayer_ player, LCUI_BOOL fALSE)
    {
        return CandyUtils.DelayAction(
            () => {
                GCHandle handle = GCHandle.Alloc(player);
                IntPtr ptr = GCHandle.ToIntPtr(handle);
                func(ptr);
            }, n_ms / 1000f);

       // return 1;
    }

    public static LCUI_BOOL LCUIKey_IsDoubleHit(int key_code, int interval_time, LCUI_BOOL right_direction)
    {
       
        if(key_code == (int)DefineConstantsGame_config.LCUIKEY_A)
        {
            if (KeyCodeTime > 0)
            {
                if(right_direction)
                {
                    KeyCodeTime = interval_time;
                    return DefineConstantsGame_control.FALSE;
                }
                return DefineConstantsGame_control.TRUE;
            }
            else
            {
               
                KeyCodeTime = interval_time;
                return DefineConstantsGame_control.FALSE;
            }
        }
        if (key_code == (int)DefineConstantsGame_config.LCUIKEY_D)
        {
            if (KeyCodeTime > 0)
            {
                if (!right_direction)
                {
                    KeyCodeTime = interval_time;
                    return DefineConstantsGame_control.FALSE;
                }
                return DefineConstantsGame_control.TRUE;
            }
            else
            {
                KeyCodeTime = interval_time;
                return DefineConstantsGame_control.FALSE;
            }
        }
        return DefineConstantsGame_control.FALSE;
    }

    public static Dictionary<AnonymousEnum, Action<LCUI_Event, IntPtr>> keyCodeValuePairs = new Dictionary<AnonymousEnum, Action<LCUI_Event, IntPtr>> ();

    public static int LCUISysEvent_Connect(AnonymousEnum lCUI_KEYDOWN, Action<LCUI_Event, IntPtr> gameControl_KeyboardProc, object p)
    {
        keyCodeValuePairs.Add(lCUI_KEYDOWN, gameControl_KeyboardProc);
        return (int)lCUI_KEYDOWN;
    }

    public static LCUI_BOOL LCUIKey_IsHit(int key)
    {
        if (key <=0)
            return DefineConstantsGame_control.FALSE;
        else
        {
            if (UnityEngine.Input.GetKey((KeyCode)key))
            {
                return DefineConstantsGame_control.TRUE;
                //print("key: "+key+"  true");
            }
            else
            {
                return DefineConstantsGame_control.FALSE;
               // print("key: " + key + "  false");

            }
        }
    }

    public static void DEBUG_MSG(string info)
    {
       /// print(info );
    }

    public static void DEBUG_MSG(string info, int v1)
    {
       // print(info + " Value1: " + v1);
    }
    public static void DEBUG_MSG(string info, SkillData_.can_useDelegate v1)
    {
      //  print(info + " Value1: " + v1);
    }


    public static void DEBUG_MSG(string info, int v1, int v2)
    {
      ///  print(info + " Value1: " + v1 + "  Value2: " + v2);
    }

    public static void DEBUG_MSG(string info, int v1, int v2,int v3)
    {
       // print(info + " Value1: " + v1 + "  Value2: " + v2 + "  Value3: " + v3);
    }

    public static void DEBUG_MSG(string info, string info2, int v1)
    {
       // print(info + " info2: " + info2 + "  Value1: " + v1 );
    }

    public static void DEBUG_MSG(string info,string info2, int v1, int v2, int v3)
    {
        // //  print(info + " info2: " + info2 + "  Value1: " + v1 + "  Value2: " + v2 + "  Value3: " + v3);
    }

    public static void DEBUG_MSG(string info ,int v1,int v2, int y, int y_width)
    {
        // //  print(info + " Value1: "+v1 + "  Value2: " + v2);
    }

    public static void DEBUG_MSG(string v, int x, int x_width, int y, int y_width, int z, int z_width)
    {
        // print(v + " Value1: " + x + "  Value2: " + y);
    }

    public static void Game_PostMsg(GameMsg msg)
    {
        GlobalMembersGame_msg. Game_ProcMsg(msg);
    }

    public static void DEBUG_MSG(string v, string skill_name, int skill_id, int priority)
    {
       // print(v + " Value1: " + skill_name + "  Value2: " + skill_id);
    }

    public static bool LCUI_MSleep(float time)
    {
        if (time< Timer)
        {
            Timer += Time.deltaTime;
            return false;
        }
        else
        {
            Timer = 0;
            return true;
        }
    }

    public static float time(object p)
    {
        return UnityEngine.Time.time;
    }
}
